package com.abyss.bprecord;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.abyss.bprecord.Bean.BPInfoBean;
import com.abyss.bprecord.view.CustomDatePicker;
import com.abyss.bprecord.view.DateFormatUtils;
import com.abyss.bprecord.view.NumPop;
import com.abyss.bprecord.view.NumPop.onItemClickListener;
import com.abyss.greendaodemo.db.BPInfoBeanDao;
import com.abyss.greendaodemo.db.DaoSession;
import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.MaterialDialog.Builder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private CustomDatePicker mTimerPicker;
    private TextView tvTime;
    private TextView tvHigh;
    private TextView tvLow;
    private TextView tvPulse;
    private int currentHigh = 130;
    private int currentLow = 90;
    private Activity mThis = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.btn);

        final RecyclerView rv = findViewById(R.id.rv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(linearLayoutManager);
//        rv.addItemDecoration(new MyItemDecoration());
        Button btnQuery = findViewById(R.id.btnQuery);

        MyApp myApp = (MyApp) getApplication();
        DaoSession daoSession = myApp.getDaoSession();
        final BPInfoBeanDao bpInfoBeanDao = daoSession.getBPInfoBeanDao();


        final Builder builder = new Builder(this);
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_record, null);
        tvTime = inflate.findViewById(R.id.tv_time);
        tvHigh = inflate.findViewById(R.id.tv_high);
        tvLow = inflate.findViewById(R.id.tv_low);
        tvPulse = inflate.findViewById(R.id.tv_pulse);
        initListener();
        builder
                .title("血压记录")
//                .content("内容")
                .customView(inflate, true)
                .positiveText("确认")
                .negativeText("取消");
//
//
//                .show();

        initTimerPicker();
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                BPInfoBean bpInfoBean = new BPInfoBean();
//                bpInfoBean.setTime(getDateTimeFromMillisecond());
//                bpInfoBean.setSystolicPressure(130 + "");
//                bpInfoBean.setDiastolicPressure(90 + "");
//                bpInfoBean.setPulse(60 + "");
//                bpInfoBeanDao.insert(bpInfoBean);
                builder.show();
//                mTimerPicker.show(getDateTimeFromMillisecond());
            }
        });
        btnQuery.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                List<BPInfoBean> bpInfoBeans = bpInfoBeanDao.loadAll();
                rv.setAdapter(new ListAdapter(getApplicationContext(), bpInfoBeans));
//                StringBuilder sb = new StringBuilder();
//                for (BPInfoBean bpInfoBean : bpInfoBeans) {
//                    sb.append(bpInfoBean.toString());
//                    sb.append("\n");
//                }`

//                tv.setText(sb);
            }
        });
    }

    private void initListener() {
        tvTime.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mTimerPicker.show(getDateTimeFromMillisecond());
            }
        });
        tvHigh.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                NumPop numPop = new NumPop(mThis, new onItemClickListener() {
                    @Override
                    public void OnItemClicked(int num) {
                        tvHigh.setText(num + "");
                    }
                }, currentHigh, 0, 300);
                numPop.setPopupGravity(Gravity.CENTER_HORIZONTAL);
                numPop.setBackground(mThis.getResources().getDrawable(android.R.color.transparent));
                numPop.showPopupWindow(tvHigh);
            }
        });
        tvLow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mTimerPicker.show(getDateTimeFromMillisecond());
            }
        });
        tvPulse.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mTimerPicker.show(getDateTimeFromMillisecond());
            }
        });
    }

    private void initTimerPicker() {
        String beginTime = "2019-01-01 00:00";
        String endTime = DateFormatUtils.long2Str(System.currentTimeMillis(), true);

//        mTvSelectedTime.setText(endTime);

        // 通过日期字符串初始化日期，格式请用：yyyy-MM-dd HH:mm
        mTimerPicker = new CustomDatePicker(this, new CustomDatePicker.Callback() {
            @Override
            public void onTimeSelected(long timestamp) {
                tvTime.setText(DateFormatUtils.long2Str(timestamp, true));
            }
        }, beginTime, endTime);
        // 允许点击屏幕或物理返回键关闭
        mTimerPicker.setCancelable(false);
        // 显示时和分
        mTimerPicker.setCanShowPreciseTime(true);
        // 允许循环滚动
        mTimerPicker.setScrollLoop(false);
        // 允许滚动动画
        mTimerPicker.setCanShowAnim(false);
    }

    public static String getDateTimeFromMillisecond() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date(System.currentTimeMillis());
        String dateStr = simpleDateFormat.format(date);
        return dateStr;
    }
}
